package com.foreveredu.erlms.persist.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.mapping.Collection;

import com.foreveredu.erlms.domain.Examinee;
import com.foreveredu.erlms.domain.Parameter;
import com.foreveredu.erlms.persist.IExamineeDao;

public class HibernateExamineeDao extends
		HibernateAbstractDao<Examinee, Integer> implements IExamineeDao {

	private static final long serialVersionUID = 1L;

	public HibernateExamineeDao() {
		super(Examinee.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Examinee getExamineeOfMaxEnrollNumber(int examOrder) {
		Examinee examinee = null;
		List<Examinee> examineeList = this
				.getHibernateTemplate()
				.find(
						"from Examinee e where e.enrollNumber is not null and e.examOrder = ? and substring(e.enrollNumber,9,6) =  (SELECT max(substring(e.enrollNumber,9,6)) from Examinee e where e.examOrder = ?)",
						new Object[] { examOrder, examOrder });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Examinee findExamineeByIdCard(String idCard, int examOrder) {
		Examinee examinee = null;
		List<Examinee> examineeList = this.getHibernateTemplate().find(
				"from Examinee e where e.idCard = ? and e.examOrder = ?",
				new Object[] { idCard, examOrder });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Examinee getExamineeOfMaxSerialNumber(String schoolCode,
			int examOrder) {
		Examinee examinee = null;
		List<Examinee> examineeList = this
				.getHibernateTemplate()
				.find(
						"from Examinee e where e.examOrder = ? and e.serialNumber = (select max(e.serialNumber) from Examinee e where e.examOrder = ?)",
						new Object[] { examOrder, examOrder });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Examinee findExamineeBySerialNumber(String serialNumber,
			int examOrderOfNow) {
		Examinee examinee = null;
		List<Examinee> examineeList = this.getHibernateTemplate().find(
				"from Examinee e where e.serialNumber = ? and e.examOrder = ?",
				new Object[] { serialNumber, examOrderOfNow });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	public Examinee findExamineeByEnrollNumber(String enrollNumber,
			int examOrderOfNow) {
		Examinee examinee = null;
		List<Examinee> examineeList = this.getHibernateTemplate().find(
				"from Examinee e where e.enrollNumber = ? and e.examOrder= ?",
				new Object[] { enrollNumber, examOrderOfNow });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Examinee findExamineeByTicketNumber(String ticketNumber,
			int examOrderOfNow) {
		Examinee examinee = null;
		List<Examinee> examineeList = this.getHibernateTemplate().find(
				"from Examinee e where e.ticketNumber = ? and e.examOrder= ?",
				new Object[] { ticketNumber, examOrderOfNow });
		if (examineeList != null && examineeList.size() > 0) {
			examinee = examineeList.get(0);
		}
		return examinee;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> findExamOrderByExaminee() {
		List<Integer> examOrderList = new ArrayList<Integer>();
		Query query = (Query) this.getSession().createQuery(
				"select distinct e.examOrder from Examinee e");
		examOrderList.addAll(query.list());
		return examOrderList;
	}

}
